<div id="tb<?php echo $uniqid;?>" style="padding:5px">
		<table cellspacing="0" cellpadding="0"><tbody><tr>
		<td><a href="javascript:;" onclick="rankAdd();" class="l-btn l-btn-small l-btn-plain"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">增加</span><span class="l-btn-icon icon-add">&nbsp;</span></span></a></td><td><div class="datagrid-btn-separator"></div></td>
		<td><a href="javascript:;" onclick="rankEdit();"  class="l-btn l-btn-small l-btn-plain"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">编辑</span><span class="l-btn-icon icon-edit">&nbsp;</span></span></a></td>
		<td><div class="datagrid-btn-separator"></div></td>
		<td><a href="javascript:;" onclick="rankDelete();"  class="l-btn l-btn-small l-btn-plain"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">删除</span><span class="l-btn-icon icon-cancel">&nbsp;</span></span></a></td><td><div class="datagrid-btn-separator"></div></td>
		<td><a href="javascript:;" onclick="rankReload();"  class="l-btn l-btn-small l-btn-plain"><span class="l-btn-left l-btn-icon-left"><span class="l-btn-text">重载</span><span class="l-btn-icon icon-reload">&nbsp;</span></span></a></td>
		<td>&nbsp;&nbsp;<input  id="rankkeywords"  style="width:100px" type="text" class= "easyui-input textbox" > </input></td>
		<td><span><a href="javascript:void(0)" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="rankSearch()">查询</a></span>	</td>
		</tr></tbody></table>
</div>

<div class="con"  style="-moz-user-select:none;">
    <table id="rank"></table>
</div>

<div id="rankdlg" class="easyui-dialog" style="width:720px; height:460px;" title="信息框" closed="true"></div>

<script>
var rankAdd=function(){
                $("#rankdlg").dialog({    
					title: '新增信息',      
					closed: false,    
					width: 700,
					height:380,
					cache: false,    
					href: '/admin/members/rankAdd',
					modal: true,
					buttons:[{
						text:'保存',
						id:'rankaddsubmit',
						iconCls:'icon-ok',
					},{
						text:'关闭',
						iconCls:'icon-no',
						handler:function(){$("#rankdlg").dialog('close');}
					}]  
				});
};
var rankEdit=function(){
				var row = $('#rank').datagrid('getSelected');
				if (row){
						$("#rankdlg").dialog({    
							title: '编辑信息',      
							closed: false,
							width: 700,
							height:380,    
							cache: false,    
							href: '/admin/members/rankEdit?id='+row.id,
							modal: true,
							buttons:[{
								text:'保存',
								id:'rankeditsubmit',
								iconCls:'icon-ok',
							},{
								text:'关闭',
								iconCls:'icon-no',
								handler:function(){$("#rankdlg").dialog('close');}
							}]  
						});				
				}else{
						$.messager.alert('提示','请先选择要编辑的行！','warning');
				}
};
var rankDelete=function(){
				var row = $('#rank').datagrid('getSelected');
				if (row){
                    $.messager.confirm('提示','确定删除吗？',function(r){
                        if(r==true){
                            $.messager.progress();
                            $.post('/admin/members/rankDelete', {id:row.id}, function(data){
                                $.messager.progress('close');
								if(data.ret==0){
										$.messager.show({
												title:'提示',
												msg:data.msg,
												timeout:3000,
												showType:'slide'
										});
										rankReload();
								}else{
										$.messager.alert('提示',data.msg,'warning');
								}								
                            },'json');
                        }
                    });
                }else{
						$.messager.alert('提示','请先选择要删除的行！','warning');
				}
};
var rankReload=function(){
                $("#rank").datagrid('reload');
};
var rankSearch=function(){
		var height=	$(window).height()-$(".top").height()-121;
		var width = $(window).width()-$("#leftMenu").width()-13;
        $("#rank").datagrid({
            title:'基本信息',
            width:width,
            height:height,//高度
            singleSelect:true,//如果为true，则只允许选择一行
            striped:true,//是否显示斑马线效果。
            rownumbers:true,//如果为true，则显示一个行号列。
            pagination:true,//是否显示分页
            pageSize:10,
            pageList:[10,15,20,25],
            method:'post',
            sortName:'id',
            sortOrder:'asc',
            url:'/admin/members/rankGet',
			queryParams : {keywords:$('#rankkeywords').val()},
            fitColumns:true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap:true,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck:false,
            checkOnSelect:true,
			toolbar: '#tb<?php echo $uniqid;?>',			
            columns:[[
                {field:'name',title:'等级名称',width:'20%',editor:'text',styler:enableEditStyler},
				{field:'min',title:'积分最小值',width:'10%',editor:'text',styler:enableEditStyler},
                {field:'max',title:'积分最大值',width:'10%',editor:'text',styler:enableEditStyler},
                {field:'rate',title:'优惠比例（%）',width:'10%',editor:'text',styler:enableEditStyler},
				{field:'created_at',title:'创建时间',width:'15%',editor:'datetimebox'},
				{field:'updated_at',title:'更新时间',width:'15%',editor:'datetimebox'},
            ]],
			onDblClickCell: onDblClickCell,
			onEndEdit:onEndEdit,  
    	});
}
rankSearch();
var enableEditStyler=function(value,row,index){
		return 'color:gray; font-style:italic;';
}
var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#rank').datagrid('validateRow', editIndex)){
		$('#rank').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
function onDblClickCell(rowIndex, field, value){
	if (endEditing()){
			$('#rank').datagrid('selectRow', rowIndex)
					.datagrid('editCell', {index:rowIndex,field:field});
			editIndex = rowIndex;
	}
		
	$("input.datagrid-editable-input,span.datebox input.textbox-text").val(value).bind("blur",function(evt){
			var dataArry = $('#rank').datagrid('getSelections');
			$('#rank').datagrid('endEdit',rowIndex);			
	}).bind("keyup",function(evt){
		    if(evt.keyCode==13){
				   this.blur();
		    }
	}).focus();
}
$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$(this).datagrid('beginEdit', param.index);
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});
function isEmptyObject(e) {  
	var t;  
	for (t in e)  
		return !1;  
	return !0  
}  
function onEndEdit(index, row, changes) {
	if( isEmptyObject(changes) ){
		editRow = undefined;  
        $('#rank').datagrid('unselectAll');  
        return false;
	}else{
        submitForms(row.id, changes);  
	}	
}  
function submitForms(id, changes) {  
	changes.id=id;
	$.post('/admin/members/rankCellUpdate', {data:changes}, function(data){
		if(data.ret==0){
				$.messager.show({
						title:'提示',
						msg:data.msg,
						timeout:3000,
						showType:'slide'
				});		
				rankReload();		
		}else{
				$.messager.alert('提示',data.msg,'warning');
		}
	})
}
</script>